Qt.include("networkmanager.js")

oauth_data.oauth_realm = "http://api.twitter.com"
oauth_data.consumer_key = twitterConsumerKey;
oauth_data.consumer_secret = twitterConsumerSecret;
oauth_data.request_token_url = "https://api.twitter.com/oauth/request_token";
oauth_data.request_token_method = "POST"
oauth_data.authorize_token_url = "https://api.twitter.com/oauth/authorize";
oauth_data.access_token_url = "https://api.twitter.com/oauth/access_token";
oauth_data.access_token_method = "POST"
oauth_data.callback_url = "";
//oauth_data.callback_url = "http://twitter.com/metlak";
oauth_data.oauth_token = "";
oauth_data.oauth_token_secret = "";
oauth_data.auth_method = "HEADER";

var twitter = {}
twitter.api_root = "http://api.twitter.com";
twitter.api_version = "1";

twitter.statuses = {}
twitter.statuses.update_url = twitter.api_root + "/" + twitter.api_version + "/statuses/update.xml"

function getAuthorizationHeader(url, method, parameters) {
    loadAuthInfo(oauth_data.oauth_realm);
    var accessor = {
      token: oauth_data.oauth_token,
      tokenSecret: oauth_data.oauth_token_secret,
      consumerKey : oauth_data.consumer_key,
      consumerSecret: oauth_data.consumer_secret
    };

    var message = {
      "action": url,
      "method": method,
      "parameters": parameters
    };

    OAuth.completeRequest(message, accessor);

    return OAuth.getAuthorizationHeader(oauth_data.oauth_realm, parameters);    // no realm?
}

/*
  create request for status update
  @return xhr object
  */
twitter.statuses.update = function(message) {
    var xhr = new XMLHttpRequest();
    var method = "POST";
    var url = twitter.statuses.update_url;
    var parameters = { status: message }
    var header = getAuthorizationHeader(url, method, parameters);

    console.log("HEADER: " + header);

    url += "?status=" + parameters.status;

    console.log(url);

    xhr.open(method, url);
    xhr.setRequestHeader("Authorization", header)
    return xhr;
}
